home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2b.lha / p4-1.2b / lib / p4_sock_conn.c < prev    next >
C/C++ Source or Header  |  1993-02-06  |  6KB  |  203 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. int establish_connection(dest_id)
  5. int dest_id;
  6. {
  7.     int myid = p4_get_my_id();
  8.  
  9.     p4_global->dest_id[myid] = dest_id;
  10.     request_connection(dest_id);
  11.     p4_global->dest_id[myid] = (-1);
  12.  
  13.     if (myid > dest_id)
  14.     {
  15.     /* following should not spin long */
  16.     while (p4_local->conntab[dest_id].type == CONN_REMOTE_NON_EST)
  17.         p4_dprintfl(70, "waiting for interrupt handler to do its job\n");
  18.     }
  19.     return (TRUE);
  20. }
  21.  
  22.  
  23. P4VOID request_connection(dest_id)
  24. int dest_id;
  25. {
  26.     struct proc_info *my_pi, *dest_pi;
  27.     char *my_host, *dest_host;
  28.     int my_id;
  29.     struct slave_listener_msg msg;
  30.     int connection_fd;
  31.     int dest_listener_con_fd;
  32.     int connected_to_dest_listener;
  33.     int my_listener, dest_listener;
  34.     int new_listener_port, new_listener_fd;
  35.     int oldmask;
  36.     int num_tries;
  37.  
  38. #   ifdef P4SYSV
  39.     sighold(LISTENER_ATTN_SIGNAL);
  40. #   else
  41.     oldmask = sigblock(sigmask(LISTENER_ATTN_SIGNAL));
  42. #   endif
  43.  
  44.     /* Get some initial information */
  45.     my_id = p4_get_my_id();
  46.     my_pi = get_proc_info(my_id);
  47.     my_host = my_pi->host_name;
  48.     my_listener = my_pi->port;
  49.  
  50.     dest_pi = get_proc_info(dest_id);
  51.     dest_host = dest_pi->host_name;
  52.     dest_listener = dest_pi->port;
  53.  
  54.     p4_dprintfl(70, "request_connection: my_id=%d my_host=%s my_listener=%d dest_id=%d dest_host=%s dest_listener=%d\n",
  55.         my_id, my_host, my_listener, dest_id, dest_host, dest_listener);
  56.  
  57.     /* Have we already connected?? */
  58.     if (p4_local->conntab[dest_id].type == CONN_REMOTE_EST)
  59.     {
  60.     p4_dprintf("request_connection %d: already connected\n", dest_id);
  61. #       ifdef P4SYSV
  62.         sigrelse(LISTENER_ATTN_SIGNAL);
  63. #       else
  64.     sigsetmask(oldmask);
  65. #       endif
  66.     return;
  67.     }
  68.  
  69.     p4_dprintfl(70, "enter loop to connect to dest listener %s\n",dest_host);
  70.     /* Connect to dest listener */
  71.     num_tries = 1;
  72.     while((dest_listener_con_fd = net_conn_to_listener(dest_host,dest_listener,1)) == -1)
  73.     num_tries++;
  74.     p4_dprintfl(70, "conn_to_proc_contd: connected after %d tries, dest_listener_con_fd=%d\n",num_tries, dest_listener_con_fd);
  75.  
  76.  
  77.     /* Setup a listener to accept the connection to dest_id */
  78.     net_setup_anon_listener(1, &new_listener_port, &new_listener_fd);
  79.  
  80.     /* Construct a connection request message */
  81.     msg.type = p4_i_to_n(CONNECTION_REQUEST);
  82.     msg.from = p4_i_to_n(my_id);
  83.     msg.lport = p4_i_to_n(new_listener_port);
  84.     msg.to = p4_i_to_n(dest_id);
  85.     msg.to_pid = p4_i_to_n(dest_pi->unix_id);
  86.  
  87.     /* Send it to dest_id's listener */
  88.     p4_dprintfl(70, "request_connection: sending CONNECTION_REQUEST to %d on fd=%d size=%d\n",
  89.         dest_id,dest_listener_con_fd,sizeof(msg));
  90.     net_send(dest_listener_con_fd, &msg, sizeof(msg), FALSE);
  91.     p4_dprintfl(70, "request_connection: sent CONNECTION_REQUEST to dest_listener\n");
  92.  
  93.     if (my_id < dest_id)
  94.     {
  95.     /* Wait for the remote process to connect to me */
  96.     p4_dprintfl(70, "request_connection: waiting for accept from %d \n",dest_id);
  97.     connection_fd = net_accept(new_listener_fd);
  98.     p4_dprintfl(70, "request_connection: accepted from %d on %d\n", dest_id, connection_fd);
  99.  
  100.     /* Add the connection to the table */
  101.     p4_local->conntab[dest_id].type = CONN_REMOTE_EST;
  102.     p4_local->conntab[dest_id].port = connection_fd;
  103.     p4_local->conntab[dest_id].same_data_rep =
  104.         same_data_representation(p4_local->my_id,dest_id);
  105.     }
  106.  
  107.     close(dest_listener_con_fd);
  108.     /* Now release the listener connections */
  109.     close(new_listener_fd);
  110.  
  111. #   ifdef P4SYSV
  112.     sigrelse(LISTENER_ATTN_SIGNAL);
  113. #   else
  114.     sigsetmask(oldmask);
  115. #   endif
  116.  
  117.     p4_dprintfl(70, "request_connection: finished connecting\n");
  118.     return;
  119. }
  120.  
  121. P4VOID handle_connection_interrupt()
  122. {
  123.     struct slave_listener_msg msg;
  124.     int type;
  125.     int listener_fd;
  126.     int to, to_pid, from, lport;
  127.     int connection_fd;
  128.     struct proc_info *from_pi;
  129.     int myid = p4_get_my_id();
  130.     int num_tries;
  131.  
  132.     listener_fd = p4_local->listener_fd;
  133.     p4_dprintfl(70, "Inside handle_connection_interrupt, listener_fd=%d\n",
  134.         listener_fd);
  135.  
  136.     if (net_recv(listener_fd, &msg, sizeof(msg)) == PRECV_EOF)
  137.     {
  138.     p4_dprintf("OOPS: got eof in handle_connection_interrupt\n");
  139.     return;
  140.     }
  141.  
  142.     type = p4_n_to_i(msg.type);
  143.     if (type != CONNECTION_REQUEST)
  144.     {
  145.     p4_dprintf("handle_connection_interrupt: invalid type %d\n", type);
  146.     return;
  147.     }
  148.  
  149.     to = p4_n_to_i(msg.to);
  150.     from = p4_n_to_i(msg.from);
  151.     to_pid = p4_n_to_i(msg.to_pid);
  152.     lport = p4_n_to_i(msg.lport);
  153.  
  154.     p4_dprintfl(70, "handle_connection_interrupt: msg contents: to=%d from=%d to_pid=%d lport=%d\n",
  155.         to, from, to_pid, lport);
  156.  
  157.     /* If we're already connected, forget about the interrupt. */
  158.     if (p4_local->conntab[from].type != CONN_REMOTE_EST)
  159.     {
  160.     if (myid < from)
  161.     {
  162.         /* see if I have already started this connection */
  163.         p4_dprintfl(90,"myid < from, myid = %d, from = %d\n",myid,from);
  164.         if (p4_global->dest_id[myid] != from)
  165.         request_connection(from);
  166.     }
  167.     else
  168.     {
  169.         /* Get the information for the process we're connecting to */
  170.         from_pi = &(p4_global->proctable[from]);
  171.  
  172.         /* Connect to the waiting process */
  173.         p4_dprintfl(70, "connecting to port...\n");
  174.         num_tries = 1;
  175.         /* connect to the requesting process, who is listening */
  176.         p4_dprintfl(70,"handling connection interrupt: connecting to %s\n",from_pi->host_name);
  177.         while ((connection_fd = net_conn_to_listener(from_pi->host_name,lport,1)) == -1)
  178.         num_tries++;
  179.  
  180.         p4_dprintfl(70, "handling connection interrupt: connected after %d tries, connection_fd=%d host = %s\n",
  181.             num_tries, connection_fd, from_pi->host_name);
  182.  
  183.         /* We're connected, so we can add this connection to the table */
  184.         p4_local->conntab[from].type = CONN_REMOTE_EST;
  185.         p4_local->conntab[from].port = connection_fd;
  186.         p4_local->conntab[from].same_data_rep =
  187.         same_data_representation(p4_local->my_id,from);
  188.         p4_dprintfl(70, "marked as live fd=%d from=%d\n", connection_fd, from);
  189.     }
  190.     }
  191.     else
  192.     {
  193.     p4_dprintfl(70,"ignoring interrupt from %d\n",from);
  194.     }
  195.  
  196.     msg.type = p4_i_to_n(IGNORE_THIS);
  197.     p4_dprintfl(70, "handle_connection_interrupt: sending IGNORE_THIS to my_listener\n");
  198.     /* send msg to listener indicating I made the connection */
  199.     net_send(listener_fd, &msg, sizeof(msg), FALSE);
  200.     p4_dprintfl(70, "handle_connection_interrupt: exiting handling intr from %d\n",from);
  201.     SIGNAL_P4(LISTENER_ATTN_SIGNAL, handle_connection_interrupt);
  202. }
  203.